package com.imooc.ecommerce.advice;

import com.imooc.ecommerce.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author Mr.Keshihao
 * @Date 2021/12/1 15:54
 * @Version 1.0
 * 全局异常捕获处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {

  @ExceptionHandler(value = Exception.class) // 全局异常拦截注解 value可以指定自定义异常捕获
  public CommonResponse<String> handlerCommerceException(
    HttpServletRequest req, Exception ex
  ) {
    CommonResponse<String> response = new CommonResponse<>(-1,"business error");
    response.setData(ex.getMessage());
    log.error("commerce service has error: [{}]",ex.getMessage(),ex);
    return response;
  }

}
